package com.ldq.imooc.db;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @Author : 李道泉
 * @Description : 访问数据库
 * @Date : 2018/8/28 20:31
 * @Modified by :
 */
public class DBAccess {

    /**
     * @Author : 李道泉
     * @Description :
     * @Date 2018/8/28 20:38
     * @Modified by :
     */
    public SqlSession getSqlSession() throws IOException {

        //1、通过配置文件获取数据库连接相关信息
        Reader reader = Resources.getResourceAsReader("com/ldq/imooc/config/Configuration.xml");
        //2、通过配置信息构建SQLSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //3、通过SQLSessionFactory打开数据库会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
